package com.example.filesaveqq;

import android.content.ContentValues;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_acount, et_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始控件
        initView();
        //读取保存的密码
        Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);
        if (userInfo != null) {
            et_acount.setText(userInfo.get("account"));
            et_password.setText(userInfo.get("password"));
        }
        Bundle userInfo1=FileSaveQQ.getUserInfo1(this);
        if(userInfo1.size()!=0){
            et_acount.setText(userInfo1.getString("account",""));
            et_password.setText(userInfo1.getString("password",""));
        }
        //Contentvalues
        ContentValues userinfo2=FileSaveQQ.getUserinfo2(this);
        if(userinfo2.size()!=0){
            et_acount.setText(userinfo2.getAsString("account"));
            et_password.setText(userinfo2.getAsString("password"));
        }
    }

    private void initView() {
        et_acount = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                String account = et_acount.getText().toString().trim();
                String password = et_password.getText().toString().trim();
                if (TextUtils.isEmpty(account)) {
                    showToast("请输入QQ号");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    showToast("请输入密码");
                    return;
                }
                showToast("登录成功！");
                boolean isSavedSuccess = FileSaveQQ.saveUserInfo(this, account, password);
                if (isSavedSuccess) {
                    showToast("账号和密码保存成功！");
                } else {
                    showToast("账号和密码保存失败！");
                }
                break;
        }
    }

    private void showToast(String string) {
        if (string.length() != 0) {
            Toast.makeText(this, string, Toast.LENGTH_LONG).show();
        }
    }
}
